package com.lsht.system.service.impl.siwei;

import com.lsht.common.config.LshtConfig;
import com.lsht.common.core.domain.AjaxResult;
import com.lsht.common.utils.FileUploadTool;
import com.lsht.common.word.AsposeWordUtil;
import com.lsht.system.domain.siwei.BusHelpFile;
import com.lsht.system.mapper.siwei.BusHelpFileMapper;
import com.lsht.system.service.BusHelpFileService;
import com.lsht.system.util.NullUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

import static com.lsht.common.word.AsposeWordUtil.*;

/**
 * @author zyc
 * @date 2023/10/11 14:48
 */
@Service
public class BusHelpFileServiceImpl implements BusHelpFileService {
    @Autowired
    private BusHelpFileMapper busHelpFileMapper;

    /**
     * 新增帮助文档
     *
     * @param busHelpFile 帮助文档
     * @param file
     * @return 结果
     */
    @SneakyThrows
    @Override
    public AjaxResult genAdd(BusHelpFile busHelpFile, MultipartFile file) {
        //设置地图上传路径
        final String logoUrl = LshtConfig.getProfile() + File.separator + "help" + File.separator;
        if (NullUtil.isNotEmpty(file)) {
            //设置文件路径
            String originalFilename = file.getOriginalFilename();
            //根据文件名称判断文件是否存在
            if (NullUtil.isEmpty(busHelpFile.getId())) {
                Integer count = busHelpFileMapper.extisHelpFile(originalFilename, busHelpFile.getFileType());
                if (count >= 1) {
                    return AjaxResult.error(originalFilename + "已存在");
                }
            }
            //获取文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            busHelpFile.setFileSuffix(suffix);
            busHelpFile.setFileName(originalFilename);
            // 上传并返回新文件名称
            String fileName = FileUploadTool.upload(logoUrl, file, originalFilename);
            String pdfPath = "";
            String replace = logoUrl.replace(LshtConfig.getProfile(), "/profile") + originalFilename;
            if (suffix.contains(".doc") || suffix.contains(".docx")) {
                //word转dpf
                String s = replace.replaceAll("\\\\", "/");
                String replace1 = s.replace("/profile", LshtConfig.getProfile());
                if (suffix.equals(".doc")) {
                    pdfPath = replace1.replace(".doc", ".pdf");
                } else {
                    pdfPath = replace1.replace(".docx", ".pdf");
                }
                AsposeWordUtil.doc2pdf(replace1, pdfPath);
                String replace2 = pdfPath.replace(LshtConfig.getProfile(), "/profile");
                busHelpFile.setFilePdfUrl(replace2.replaceAll("\\\\", "/"));
            } else if (suffix.contains(".xls") || suffix.contains(".xlsx")) {
                //excel转pdf
                String s = replace.replaceAll("\\\\", "/");
                String replace1 = s.replace("/profile", LshtConfig.getProfile());
                //去除后缀
                // 找到最后一个文件分隔符的索引
                int lastSeparatorIndex = replace1.lastIndexOf("/") != -1 ? replace1.lastIndexOf("/") : replace1.lastIndexOf("\\");
                // 提取路径部分
                String path = replace1.substring(0, lastSeparatorIndex);
                String ext = "";
                if (suffix.equals(".xls")) {
                    pdfPath = replace1.replace(".xls", ".pdf");
                    ext = "xls";
                } else {
                    pdfPath = replace1.replace(".xlsx", ".pdf");
                    ext = "xlsx";
                }
                excel2pdf(replace1, path, ext);
                String replace2 = pdfPath.replace(LshtConfig.getProfile(), "/profile");
                busHelpFile.setFilePdfUrl(replace2.replaceAll("\\\\", "/"));
            } else if (suffix.contains(".ppt") || suffix.contains(".pptx")) {
                String s = replace.replaceAll("\\\\", "/");
                String replace1 = s.replace("/profile", LshtConfig.getProfile());
                int lastSeparatorIndex = replace1.lastIndexOf("/") != -1 ? replace1.lastIndexOf("/") : replace1.lastIndexOf("\\");
                // 提取路径部分
                String path = replace1.substring(0, lastSeparatorIndex);
                String end = "";
                String newPath1 = path.replace("/", File.separator);
                if (suffix.equals(".ppt")) {
                    pptToPdf(replace1.replace("/", File.separator), newPath1 + File.separator);
                    end = ".ppt";
                } else {
                    pptxToPdf(replace1.replace("/", File.separator), newPath1 + File.separator);
                    end = ".pptx";
                }
                String s1 = replace.replaceAll("\\\\", "/");
                busHelpFile.setFilePdfUrl(s1.replace(end, ".pdf"));
            } else if (suffix.contains(".txt")) {
                String s = replace.replaceAll("\\\\", "/");
                String replace1 = s.replace("/profile", LshtConfig.getProfile());
                //去除后缀
                // 找到最后一个文件分隔符的索引
                int lastSeparatorIndex = replace1.lastIndexOf("/") != -1 ? replace1.lastIndexOf("/") : replace1.lastIndexOf("\\");
                // 提取路径部分
                String path = replace1.substring(0, lastSeparatorIndex);
                txtToPdf(replace1, replace1.replace(".txt", ".pdf"));
            } else {
                busHelpFile.setFilePdfUrl(replace.replaceAll("\\\\", "/"));
            }
            busHelpFile.setFileUrl(replace.replaceAll("\\\\", "/"));
        }
        busHelpFile.setCreateDate(new Date());
        //id不为空走修改
        if (NullUtil.isNotEmpty(busHelpFile.getId())) {
            busHelpFile.setUpdateDate(new Date());
            busHelpFileMapper.genUpdateById(busHelpFile);
        }
        int i = busHelpFileMapper.genAdd(busHelpFile);
        if (i != 0) {
            return AjaxResult.success("上传成功");
        }
        return AjaxResult.error("上传失败");
    }

    /**
     * 删除帮助文档信息
     *
     * @param id 帮助文档主键
     * @return 结果
     */
    @Override
    public int genDeleteById(List<String> idList) {
        return busHelpFileMapper.genDeleteById(idList);
    }

    /**
     * 修改帮助文档
     *
     * @param busHelpFile 帮助文档
     * @return 结果
     */
    @Override
    public int genUpdateById(BusHelpFile busHelpFile) {
        return busHelpFileMapper.genUpdateById(busHelpFile);
    }

    /**
     * 查询帮助文档列表
     *
     * @param busHelpFile 帮助文档
     * @return 帮助文档
     */
    @Override
    public List<BusHelpFile> genQueryList(BusHelpFile busHelpFile) {
        return busHelpFileMapper.genQueryList(busHelpFile);
    }
}
